﻿using System.Windows;
using FrameWork.Language;
using System.Windows.Media;
using System.Windows.Controls;

namespace FrameWork.UI.Controls
{
    public class NbUserControl : UserControl
    {
        private System.Timers.Timer _timer;
        private Border _outerBorder;
        private Border _innerBorder;
        private Image _loadImage;
        private Label _textLabel;


        public NbUserControl()
        {
            _timer = new System.Timers.Timer();
            _timer.Elapsed += _timer_Elapsed;
            _timer.Interval = 3000;
        }

        public bool IsWaiting
        {
            get
            {
                return (bool)GetValue(IsWaitingProperty);
            }
            set
            {
                SetValue(IsWaitingProperty, value);
                Waiting();
            }
        }
        public static readonly DependencyProperty IsWaitingProperty =
            DependencyProperty.Register("IsWaiting", typeof(bool), typeof(NbUserControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsWaitingPropertyCallBack));
        public static void IsWaitingPropertyCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
        {
            property.SetValue(IsWaitingProperty, args.NewValue);
            (property as NbUserControl).Waiting();
        }



        public string TipText
        {
            get { return (string)GetValue(TipTextProperty); }
            set { SetValue(TipTextProperty, value); ShowTipText(value); }
        }

        // Using a DependencyProperty as the backing store for LoadText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TipTextProperty =
            DependencyProperty.Register("TipText", typeof(string), typeof(NbUserControl), new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, TipTextPropertyCallBack));
        public static void TipTextPropertyCallBack(DependencyObject property, DependencyPropertyChangedEventArgs args)
        {
            property.SetValue(TipTextProperty, args.NewValue);
            (property as NbUserControl).ShowTipText(args.NewValue?.ToString());
        }



        public void Waiting()
        {
            if (this._outerBorder == null)
            {
                var grid = new Grid();
                this._outerBorder = new Border();
                this._outerBorder.Background = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                this._outerBorder.Opacity = 0.2;
                this._outerBorder.MouseDown += _outerBorder_MouseDown;
                this._innerBorder = new Border();
                this._innerBorder.Background = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                this._innerBorder.Opacity = 0.6;
                this._innerBorder.Height = 100;
                this._innerBorder.Width = 100;
                this._innerBorder.CornerRadius = new System.Windows.CornerRadius(5);

                _textLabel = new Label();
                _textLabel.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                _textLabel.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                _textLabel.Foreground = new SolidColorBrush(Color.FromRgb(0xFF, 0xFF, 0xFF));

                //_loadImage = new Image() { Width = 50, Height = 50 };
                //ImageBehavior.SetAnimatedSource(_loadImage, new BitmapImage(new Uri(@"/Parking.UI;Component/Styles/Default/waiting.gif", UriKind.RelativeOrAbsolute)));

                var pageContent = this.Content as UIElement;
                this.Content = null;
                grid.Children.Add(pageContent);
                grid.Children.Add(this._outerBorder);
                grid.Children.Add(this._innerBorder);
                this.Content = grid;
            }

            if (this.IsWaiting)
            {
                _innerBorder.Child = _loadImage;
                this._outerBorder.Visibility = Visibility.Visible;
                this._innerBorder.Visibility = Visibility.Visible;
            }
            else
            {
                this._outerBorder.Visibility = Visibility.Collapsed;
                this._innerBorder.Visibility = Visibility.Collapsed;
            }
        }

        public void ShowTipText(string text)
        {
            if (this._outerBorder == null)
            {
                var grid = new Grid();
                this._outerBorder = new Border();
                this._outerBorder.Background = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                this._outerBorder.Opacity = 0.2;
                this._outerBorder.MouseDown += _outerBorder_MouseDown;
                this._innerBorder = new Border();
                this._innerBorder.Background = new SolidColorBrush(Color.FromRgb(0, 0, 0));
                this._innerBorder.Opacity = 0.6;
                this._innerBorder.Height = 100;
                this._innerBorder.Width = 100;
                this._innerBorder.CornerRadius = new System.Windows.CornerRadius(5);

                _textLabel = new Label();
                _textLabel.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                _textLabel.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                _textLabel.Foreground = new SolidColorBrush(Color.FromRgb(0xFF, 0xFF, 0xFF));

                var pageContent = this.Content as UIElement;
                this.Content = null;
                grid.Children.Add(pageContent);
                grid.Children.Add(this._outerBorder);
                grid.Children.Add(this._innerBorder);
                this.Content = grid;
            }


            _textLabel.Content = I18n.Get(text);

            if (!string.IsNullOrWhiteSpace(text))
            {
                if (this.IsWaiting)
                    this.IsWaiting = false;

                _timer.Enabled = true;
                this._innerBorder.Child = _textLabel;
                this._outerBorder.Visibility = Visibility.Visible;
                this._innerBorder.Visibility = Visibility.Visible;
            }
            else if (!this.IsWaiting)
            {
                this._outerBorder.Visibility = Visibility.Collapsed;
                this._innerBorder.Visibility = Visibility.Collapsed;
            }
        }

        private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            _timer.Enabled = false;
            Application.Current.Dispatcher.Invoke(() =>
            {
                if (!string.IsNullOrWhiteSpace(TipText))
                {
                    TipText = string.Empty;
                }
            });
        }

        private void _outerBorder_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(TipText))
            {
                TipText = string.Empty;
            }
        }

        public void Init()
        {
            //var vm = this.DataContext as NbPageVm;
            //if (vm == null)
            //    return;
            //vm.Init();
        }
    }
}
